package com.google.coffeeshop.gui.desktop.employee;

import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JScrollPane;

import com.google.coffeeshop.business.ManagerBusiness;
import com.google.coffeeshop.entity.EmployeeDTO;
import com.google.coffeeshop.gui.desktop.ContentBasePanel;

public class EmployeePanel extends ContentBasePanel {
	public static int action = 1;
	
	public EmployeePanel(String constraints) {
		super(constraints);
		setLayout(new BorderLayout(0, 0));
		initializeGUI();
		
		
	}
	
	private void initializeGUI() {
		employeeTable = new EmployeeTable();
		employeeTable.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					EmployeeTable target = (EmployeeTable) e.getSource();
					EmployeeDTO employee = target.getEmployeeSelected();
					showAddEditEmployeeFrame(employee);
				}
			}
		});
		add(new JScrollPane(employeeTable), BorderLayout.CENTER);
		putAllEmployeesToTable(employeeTable);		
	}

	private void showAddEditEmployeeFrame(EmployeeDTO employee) {
		AddEditEmployeeFrame frmAddEditEmployee = new AddEditEmployeeFrame(this, employee);
		frmAddEditEmployee.setVisible(Boolean.TRUE);
	}
	
	public void refreshGUI() {
		removeAll();
		initializeGUI();
		validate();
		repaint();
	}
	
	private void putAllEmployeesToTable(EmployeeTable employeeTable) {
		employeeTable.addNewRows(ManagerBusiness.findAllEmployee());
	}

	public EmployeeDTO getEmployeeSelected() {
		return employeeTable.getEmployeeSelected();
	}
	
	private static final long serialVersionUID = 1L;
	private EmployeeTable employeeTable;

}
